
package com.insigma.business.qrcode.soldier.service.impl;



import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.soldier.dao.CompanyEntryApplyDao;
import com.insigma.business.qrcode.soldier.dto.CompanyEntryApplyBSearcher;
import com.insigma.business.qrcode.soldier.entity.CompanyEntryApplyDO;
import com.insigma.business.qrcode.soldier.entity.DataStatsCDO;
import com.insigma.business.qrcode.soldier.service.CompanyEntryApplyService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service("companyEntryApplyService")
public class CompanyEntryApplyServiceImpl implements CompanyEntryApplyService {

    @Autowired(required = false)
    private CompanyEntryApplyDao companyEntryApplyDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    
    @Override
    public List<CompanyEntryApplyDO> search(final CompanyEntryApplyBSearcher companyEntryApplyBSearcher) {
        if (Strings.isNotBlank(companyEntryApplyBSearcher.getAdminAreaCode())) {
            companyEntryApplyBSearcher.setAdminAreaCode(AdminAreaCodeUtils.getCutdAreaCode(companyEntryApplyBSearcher.getAdminAreaCode()) + "%");
        }
        if (Strings.isNotBlank(companyEntryApplyBSearcher.getGuideUnitCode())) {
            companyEntryApplyBSearcher.setGuideUnitCode(AdminAreaCodeUtils.getCutdAreaCode(companyEntryApplyBSearcher.getGuideUnitCode()) + "%");
        }

        return companyEntryApplyDao.search(companyEntryApplyBSearcher);
    }

    
    @Override
    public CompanyEntryApplyDO get(final String id) {
        return companyEntryApplyDao.get(id);
    }

    
    @Override
    public Integer insert(final CompanyEntryApplyDO companyEntryApplyDO) {
        companyEntryApplyDO.setId(sequenceGenerator.genKey("COMPANY_ENTRY_APPLY_ID"));
        return companyEntryApplyDao.insert(companyEntryApplyDO);
    }

    
    @Override
    public Integer update(final CompanyEntryApplyDO companyEntryApplyDO) {
        return companyEntryApplyDao.update(companyEntryApplyDO);
    }

    
    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId) {
        return companyEntryApplyDao.delete(id, modifierType, modifierId);
    }

    @Override
    public Integer countByArea(String adminAreaCode) {
        return companyEntryApplyDao.countByArea(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public Integer countByCreateDatetimeLike(String adminAreaCode, String month) {
        Integer integer = companyEntryApplyDao.countByCreateDatetimeLike(adminAreaCode, month);
        if (integer == null) {
            return 0;
        }
        return integer;
    }

    @Override
    public List<DataStatsCDO> groupByGuidUnitCode(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return companyEntryApplyDao.groupByGuidUnitCode(adminAreaCode, levelLength);
    }

    @Override
    public Integer countByBetween(String adminAreaCode, Date startTime, Date endTime) {
        return companyEntryApplyDao.countByBetween(adminAreaCode, startTime, endTime);
    }

}
